<template>
	<view class="message-container">
		<view class="status-bar"></view>
		<!-- 使用uView的下拉刷新和上拉加载组件 -->
		<scroll-view class="scroll-view" scroll-y @scrolltolower="loadMore" @refresherrefresh="refresh"
			refresher-enabled :refresher-triggered="isRefreshing" lower-threshold="50">
			<view class="message-list">
				<view class="message-item" v-for="(item, index) in messageList" :key="index" @click="viewMessage(item)">
					<view class="message-header">
						<view class="message-title">
							<text>{{ item.title }}</text>
							<!-- <view class="message-badge" v-if="!item.isRead"></view> -->
						</view>
						<text class="message-time">{{ item.creatorTime }}</text>
					</view>
					<view class="message-content">
						<text class="message-desc">{{ item.msgContent }}</text>
					</view>
				</view>
			</view>

			<!-- 上拉加载更多 -->
			<uni-load-more :status="loadMoreStatus" :contentText="contentText"></uni-load-more>
		</scroll-view>

		<!-- 空数据提示 -->
		<view class="empty-box" v-if="messageList.length === 0 && !isLoading">
			<image class="empty-image" src="/static/images/empty.png" mode="aspectFit"></image>
			<text class="empty-text">暂无消息通知</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 消息列表
				messageList: [],
				// 分页参数
				pageNum: 1,
				pageSize: 10,
				total: 0,
				// 加载状态
				isLoading: false,
				isRefreshing: false,
				loadMoreStatus: 'more', // more-加载前 loading-加载中 noMore-没有更多了
				// 加载更多文字提示
				contentText: {
					contentdown: '上拉显示更多',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				}
			}
		},
		onLoad() {
			this.getMessageList()
			
		},
		methods: {
			// 获取消息列表
			getMessageList(type = 'add') {
				if (this.isLoading) return

				this.isLoading = true
				
				var query = {pageNum:this.pageNum,pageSize:this.pageSize}
				this.$u.api.getMessage(query).then(res => {
					this.total = res.data.pagination.total
					
					// 根据类型处理数据
					if (type === 'refresh') {
						this.messageList = res.data.list
						this.isRefreshing = false
					} else {
						this.messageList = [...this.messageList, ...res.data.list]
					}
					
					// 判断是否还有更多数据
					if (this.messageList.length >= this.total) {
						this.loadMoreStatus = 'noMore'
					} else {
						this.loadMoreStatus = 'more'
					}
					
					this.isLoading = false
				})
			},

			// 下拉刷新
			refresh() {
				this.isRefreshing = true
				this.pageNum = 1
				this.loadMoreStatus = 'more'
				this.getMessageList('refresh')
			},

			// 上拉加载更多
			loadMore() {
				if (this.loadMoreStatus !== 'more') return

				this.loadMoreStatus = 'loading'
				this.pageNum += 1
				this.getMessageList()
			},

			// 查看消息详情
			viewMessage(item) {
				// 标记为已读
				const index = this.messageList.findIndex(msg => msg.id === item.id)
				if (index !== -1) {
					this.messageList[index].isRead = true
				}

				// 跳转到消息详情页
				// uni.showToast({
				// 	title: '查看消息：' + item.title,
				// 	icon: 'none'
				// })
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f5f5;
	}

	.message-container {
		display: flex;
		flex-direction: column;
		height: 100vh;
	}

	.status-bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	.nav-bar {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 44px;
		background-color: #ffffff;
		position: relative;
		box-shadow: 0 1px 5px rgba(0, 0, 0, 0.05);
	}

	.nav-bar-title {
		font-size: 18px;
		font-weight: 500;
		color: #333333;
	}

	.scroll-view {
		flex: 1;
		width: 100%;
	}

	.message-list {
		padding: 10px;
	}

	.message-item {
		background-color: #ffffff;
		border-radius: 8px;
		padding: 16px;
		margin-bottom: 10px;
		box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
	}

	.message-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 10px;
	}

	.message-title {
		display: flex;
		align-items: center;
		font-size: 16px;
		font-weight: 500;
		color: #333333;
	}

	.message-badge {
		width: 8px;
		height: 8px;
		border-radius: 50%;
		background-color: #ff4d4f;
		margin-left: 6px;
	}

	.message-time {
		font-size: 12px;
		color: #999999;
	}

	.message-content {
		color: #666666;
		font-size: 14px;
		line-height: 1.5;
	}

	.empty-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}

	.empty-image {
		width: 120px;
		height: 120px;
		margin-bottom: 16px;
	}

	.empty-text {
		font-size: 14px;
		color: #999999;
	}
</style>
